<?php
App::uses('AppController', 'Controller');
/**
 * Projetos Controller
 *
 * @property Projeto $Projeto
 */
class ProjetosController extends AppController {

	public function beforeFilter(){
		parent::beforeFilter();
		$this->usuarioSessao->install($this->usuarioAtual);
	}

	public function index() {
		$this->Projeto->recursive = 0;
		$projetos = $this->Projeto->User->find('first', array('conditions'=>array('User.id_users'=>$this->usuarioAtual['id_users'])));
		$this->set('projetos', $projetos['Projeto']);
	}


	public function view($id = null) {
		if(!$this->usuarioSessao->authProjeto($id)) return false;
		$this->Projeto->id = $id;
		if (!$this->Projeto->exists()) {
			throw new NotFoundException(__('Projeto inválido'));
		}
		$this->set('projeto', $this->Projeto->read(null, $id));
	}
	
	/***************************** ADMIN **********************************************/
	
	public function admin_index() {
		if(!$this->isAdmin()) return false;
		$this->Projeto->recursive = 0;
		$this->set('projetos', $this->paginate());
	}


	public function admin_view($id = null) {
		if(!$this->isAdmin()) return false;
		$this->Projeto->id = $id;
		if (!$this->Projeto->exists()) {
			throw new NotFoundException(__('Invalid projeto'));
		}
		$this->set('projeto', $this->Projeto->read(null, $id));
	}


	public function admin_add() {
		if(!$this->isAdmin()) return false;
		if ($this->request->is('post')) {
			$this->Projeto->create();
			if ($this->Projeto->save($this->request->data)) {
				$this->Session->setFlash(__('The projeto has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The projeto could not be saved. Please, try again.'));
			}
		}
		$empresas = $this->Projeto->Empresa->find('list');
		$users = $this->Projeto->User->find('list');
		$this->set(compact('empresas', 'users'));
	}

	public function admin_edit($id = null) {
		if(!$this->isAdmin()) return false;
		$this->Projeto->id = $id;
		if (!$this->Projeto->exists()) {
			throw new NotFoundException(__('Invalid projeto'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Projeto->save($this->request->data)) {
				$this->Session->setFlash(__('The projeto has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The projeto could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Projeto->read(null, $id);
		}
		$empresas = $this->Projeto->Empresa->find('list');
		$users = $this->Projeto->User->find('list');
		$this->set(compact('empresas', 'users'));
	}


	public function admin_delete($id = null) {
		if(!$this->isAdmin()) return false;
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Projeto->id = $id;
		if (!$this->Projeto->exists()) {
			throw new NotFoundException(__('Invalid projeto'));
		}
		if ($this->Projeto->delete()) {
			$this->Session->setFlash(__('Projeto deleted'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Projeto was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function admin_estatisticas($id){
		$this->estatisticas($id);
		$this->render('estatisticas');
	}
	
	public function estatisticas($id = null){
		$this->Projeto->id = $id;
		if (!$this->Projeto->exists()) {
			throw new NotFoundException(__('Invalid projeto'));
		}
		$this->loadModel('Tarefa');
		$status = $this->Tarefa->Status->find('list');
		$num_tarefas = 0;
		foreach ($status as $key=>$item){
			$total_tarefa = $this->Tarefa->find('count', array('conditions'=>array('projetos_id'=>$id, 'status_id'=>$key)));
			$totais[$key] = $total_tarefa;
			$num_tarefas+=$total_tarefa;
		}
		$this->set('status', $status);
		$this->set('totais', $totais);
		$this->set('total_tarefas', $num_tarefas);
	}
}
